package com.micolor.logr.demo.entity;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.micolor.logr.runtime.entity.LogMessage;
import com.micolor.logr.runtime.entity.StorageData;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Date;
import java.util.Map;

/**
 * @author EvanGe
 * @Type StorageDataEntity.java
 * @Desc
 * @date 2024/8/2 10:58
 */
@Data
@TableName("t_log_storage")
@NoArgsConstructor
@AllArgsConstructor
public class LogStorageDataEntity {

    @TableId(value = "log_id", type = IdType.AUTO)
    private String logId;

    @TableField("log_batch_id")
    private String logBatchId;

    /**
     * 操作人编号
     */
    @TableField("operator")
    private String operator;

    /**
     * 操作人名称
     */
    @TableField("operator_name")
    private String operatorName;

    /**
     * 操作描述
     */
    @TableField("operate_desc")
    private String operateDesc;

    /**
     * 操作类型
     */
    @TableField("operate_type")
    private String operateType;

    /**
     * 操作时间
     */
    @TableField("operate_time")
    private Date operateTime;

    /**
     * 业务编号
     */
    @TableField("biz_id")
    private String bizId;

    /**
     * 关联目标
     */
    @TableField("targets")
    private String targets;

    /**
     * 最后形成的message
     */
    @TableField("message")
    private String message;

    /**
     * 是否是分表关键
     */
    @TableField("sharding_info")
    private String shardingInfo;


    @TableField("fail_flag")
    private boolean failFlag;


    @TableField("fail_reason")
    private String failReason;

    public LogStorageDataEntity(String logId) {
        this.logId = logId;
    }

    public LogStorageDataEntity (StorageData storageData){
        this.setMessage(storageData.getMessage().toString());
        this.setBizId(storageData.getBizId());
        this.setFailFlag(storageData.isFailFlag());
        this.setFailReason(storageData.getFailReason());
        this.setOperateDesc(storageData.getOperateDesc());
        this.setOperateTime(storageData.getOperateTime());
        this.setOperateType(storageData.getOperateType());
        this.setOperator(storageData.getOperator());
        this.setOperatorName(storageData.getOperatorName());
        this.setShardingInfo(JSONObject.toJSONString(storageData.getShardingInfo()));
        this.setTargets(storageData.getTargets());
    }
}
